<template>
    <div class="mod-schedule">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.beanName" placeholder="库表名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="note"
          header-align="center"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="subscription"
          header-align="center"
          align="center"
          label="标识">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
            <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="sendRquestDelete([scope.row.id])">
                <el-button slot="reference" type="text" size="small">删除</el-button>
            </el-popconfirm>
            <el-button   type="text" size="small" @click="getDetile(scope.row)">查看数据</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding-top: 10px;text-align: right;">
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!-- 弹窗, 日志列表 -->
      <log v-if="logVisible" ref="log"></log>

      <el-dialog
        title=""
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        width="60%"
        :before-close="handleClose">
            <div style="display: flex;justify-content: space-between;">
                <div style="display: inline-block;width: 91%;">
                    <el-input disabled style="width:65%;display: inline-block;" v-model="path"></el-input>
                    <el-input style="width:34%;display: inline-block;" v-model="userInput"></el-input>
                </div>
                <el-button @click="test()">发送</el-button>
            </div>
            <div style="min-height: 350px;max-height: 650px;overflow: auto; background: #f7f7f7;margin-top: 10px;border-radius: 10px;padding: 10px;">
                <p style="padding: 0 0 10px 5px;border-bottom: 1px dashed #d1d1d1;" v-for="(item,index) in messageList" :key="index">
                  <pre>{{ item}}</pre>
                </p>
            </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
import { stringTrimLeft } from 'vue-json-editor/assets/jsoneditor'
import AddOrUpdate from './schedule-add-or-update'
import request from '@/utils/request'
    import Log from './schedule-log'
    export default {
      data () {
        return {
          messageList:[],
          path:window.SITE_CONFIG['baseUrl']+'/webSocket/realTime/',
          vPath:window.SITE_CONFIG['baseUrl']+'/webSocket/realTime/',
          str:"",
          newsPath:"",
          dataForm: {
            beanName: ''
          },
          userInput:"",
          dataList: [],
          dialogVisible:false,
          pageIndex: 1,
          pageSize: 10,
          totalPage: 0,
          dataListLoading: false,
          dataListSelections: [],
          addOrUpdateVisible: false,
          logVisible: false,
          message:""
        }
      },
      components: {
        AddOrUpdate,
        Log
      },
      created () {
        this.getDataList()
      },
      methods: {
        test(){
            const socket = new WebSocket('ws'+this.path.split('http')[1]);
            socket.onopen = () => {
                socket.send(JSON.stringify(this.userInput));
            };
            socket.onmessage = event => {
                this.message = event.data?JSON.parse(event.data):'';
                console.log(this.message)
                this.messageList.unshift(this.message)
            };
        },
        handleClose(){
          this.dialogVisible = false
        },
        getDetile(v){
            this.path  = this.vPath + v.subscription
            this.dialogVisible = true
        },
        // 获取数据列表
        getDataList () {
          this.dataListLoading = true
          request({
            url: '/ht-datadevelop/realTime/api/list',
            method: 'post',
            data:{
              name:this.dataForm.beanName,
              page:this.pageIndex+"",
              limit:this.pageSize+""
            }
          }).then((res) => {
            if (res.code === 200) {
              this.dataList = res.data.records
              this.totalPage = res.data.total
            } else {
              this.dataList = []
              this.totalPage = 0
            }
            this.dataListLoading = false
          })
        },
        // 每页数
        sizeChangeHandle (val) {
          this.pageSize = val
          this.pageIndex = 1
          this.getDataList()
        },
        // 当前页
        currentChangeHandle (val) {
          this.pageIndex = val
          this.getDataList()
        },
        // 多选
        selectionChangeHandle (val) {
          this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle (row) {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(row)
          })
        },
        sendRquestDelete(ids){
          request({
              url: '/ht-datadevelop/realTime/api/deleteBatch',
              method: 'post',
              data:{ids:ids}
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
        },
        // 删除
        deleteHandle (id) {
          var ids = this.dataListSelections.map(item => {
            return item.id
          })
          this.$confirm(`确定批量删除?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.sendRquestDelete(ids)
          }).catch(() => {})
        },
      }
    }
  </script>
  <style lang="scss" scoped>
  .mod-schedule{
    padding: 20px 10px;
  }
</style>